package com.scujj.cs6.controller;

import com.scujj.cs6.annotation.AuthCheck;
import com.scujj.cs6.common.Result;
import com.scujj.cs6.model.dto.question.QuestionCreateDTO;
import com.scujj.cs6.model.dto.question.QuestionUpdateDTO;
import com.scujj.cs6.model.entity.Question;
import com.scujj.cs6.model.vo.FollowQuestionVo;
import com.scujj.cs6.model.vo.PageVo;
import com.scujj.cs6.model.vo.QuestionVo;
import com.scujj.cs6.service.QuestionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "问题接口", description = "问题相关 API")
@RestController
@RequestMapping("/questions")
public class QuestionController {
    @Autowired
    private QuestionService questionService;

    //top5
    @Operation(summary = "获取TOP5新问题", description = "获取最新的5个问题")
    @GetMapping("/top5")
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    array = @ArraySchema(schema = @Schema(implementation = QuestionVo.class))
            )
    )
    public Result<List<QuestionVo>> getTop5Questions() {
        return Result.success(questionService.getTop5Questions());
    }

    @Operation(summary = "获取问题列表", description = "分页查询问题列表，支持条件搜索")

    @GetMapping ("/list")
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                   array = @ArraySchema(schema = @Schema(implementation = QuestionVo.class))
            )
    )
    public Result<PageVo<QuestionVo>> getQuestions(@Parameter(name = "page",description = "页码")
                                                @RequestParam(defaultValue = "1" , required = false) int page,
                                                 @Parameter(name = "size",description = "每页数量")
                                                @RequestParam(defaultValue = "10" , required = false) int size,
                                                 @Parameter(name = "tags",description = "标签",array = @ArraySchema(schema = @Schema(implementation = String.class)),example = "[\"java\",\"spring\"]")
                                                @RequestParam(required = false) List<String> tags,
                                                 @Parameter(name = "topic",description = "主题")
                                                @RequestParam(required = false) String topic)
                                                {
        return Result.success(questionService.getQuestions(page,size,tags,topic));
    }

    @Operation(summary = "获取我的问题列表", description = "分页查询我的问题列表")
    @GetMapping("/my")
    @AuthCheck
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    array = @ArraySchema(schema = @Schema(implementation = QuestionVo.class))
            )
    )
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    public Result<PageVo<QuestionVo>> getMyQuestions(@Parameter(name = "page",description = "页码")
                                                      @RequestParam(defaultValue = "1" , required = false) int page,
                                                  @Parameter(name = "size",description = "每页数量")
                                                  @RequestParam(defaultValue = "10" , required = false) int size, HttpServletRequest request) {
        return Result.success(questionService.getMyQuestions(page,size, request));
    }

    //获取我所关注的用户的问题列表
    @Operation(summary = "获取我关注的用户的问题列表", description = "分页查询我关注的用户的问题列表")
    @GetMapping("/follow")
    @AuthCheck
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    array = @ArraySchema(schema = @Schema(implementation = QuestionVo.class))
            )
    )
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    public Result<PageVo<FollowQuestionVo>> getFollowQuestions(@Parameter(name = "page",description = "页码")
                                                      @RequestParam(defaultValue = "1" , required = false) int page,
                                                               @Parameter(name = "size",description = "每页数量")
                                                  @RequestParam(defaultValue = "10" , required = false) int size, HttpServletRequest request) {
        return Result.success(questionService.getFollowQuestions(page,size, request));
    }


    @Operation(summary = "创建问题", description = "用户登录后可创建问题")
    @PostMapping("/")
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)
            )
    )
    public Result<Void> createQuestion(@Valid @RequestBody QuestionCreateDTO questionDTO, HttpServletRequest request) {
        questionService.createQuestion(questionDTO, request);
        return Result.success();
    }

    @Operation(summary = "获取问题详情", description = "根据问题ID获取问题详情")
    @GetMapping("/get/{id}")
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = QuestionVo.class)
            )
    )
    public Result<QuestionVo> getQuestion(@Parameter(name = "id" , description = "问题ID", required = true , in = ParameterIn.PATH) @PathVariable Long id) {
        return Result.success(questionService.getQuestionById(id));
    }


    @Operation(summary = "更新问题", description = "用户登录后可更新问题")
    @PutMapping("/{id}")
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)
            )
    )
    public Result<Void> updateQuestion(@PathVariable Long id, @Valid @RequestBody QuestionUpdateDTO questionDTO , HttpServletRequest request) {
        questionService.updateQuestion(id, questionDTO,request);
        return Result.success();
    }

    @Operation(summary = "删除问题", description = "用户登录后可删除问题")
    @DeleteMapping("/{id}")
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)
            )
    )
    public Result<Void> deleteQuestion(@Parameter(name = "id" , description = "问题ID", required = true , in = ParameterIn.PATH) @PathVariable Long id) {
        questionService.deleteQuestion(id);
        return Result.success();
    }
}
